<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<TITLE>FileHelpers Library - Marcos Meli</TITLE>
		<LINK href="/favicon.ico" rel="SHORTCUT ICON">
<link type="text/css" rel="stylesheet" href="SyntaxHighlighter.css"></link>

			<LINK href="global.css" type="text/css" rel="stylesheet">
				<meta NAME="Description" content="FileHelpers are an easy to use .NET library to read/write data from fixed length or delimited files or streams. It has support to import/export data from different data sources.">
				<meta name="Author" content="Marcos Meli">
<meta NAME="keywords" Content="filehelpers,CSV,Marcos_Meli,C#,free,opensource,Devoo.net,Delimited Files,Import/Export">
				<meta NAME="Copyright" CONTENT="Copyright 2005-07 to Marcos Meli - www.Devoo.net. All Rights Reserved.">
	</HEAD>
	<BODY text="black" vLink="purple" aLink="red" link="blue" bgColor="white" leftMargin="0"
		topMargin="0">

		{$HEADER$}


		<table width="100%" cellpadding="5" cellspacing="5">
			<tr>
				<td><div id="contentdiv">
						<h2>
                            SqlServerStorage ExtractRecords Example
						</h2>
						<blockquote>
							<p>This example shows how to read records from a sql server storage and later write
                                it
                                to a file.
							</p>
						</blockquote>
						<h2>The Problem
						</h2>
						<blockquote>
							<p>For example if you want to extract the records of the Orders table of the Northwind DB</p>
							<p><img src="orders1.png" width="652" height="374"></p>
							<p>&nbsp;</p>
						</blockquote>
						<h2>
							To use Delimited Format</h2>
						<blockquote>
							<p>You must define:</p>
							<blockquote>
<textarea name="code" class="c#">
[DelimitedRecord("|")]
public class OrdersVerticalBar
{
    public int OrderID;
    public string CustomerID;
    public int EmployeeID; 
    public DateTime OrderDate;
    public DateTime RequiredDate;
    
    [FieldNullValue(typeof(DateTime), "2005-1-1")]
    public DateTime ShippedDate; 
    public int ShipVia; 
    public decimal Freight;
}
</textarea>
							</blockquote></blockquote>
						<h2>
                            The SqlServerStorage</h2>
                            <blockquote>
						<p>
                            The idea is a lot simple you create the SqlServerStorage with your record class.</p>
                    <p>
                    
<textarea name="code" class="c#"> 
    
   SqlServerStorage storage = new SqlServerStorage(typeof(OrdersVerticalBar));
			
   storage.ServerName = "NEON-64";
   storage.DatabaseName = "Northwind";

   // Comment this for Windows Auth
   storage.UserName = "Marcos";
   storage.UserPass = "private :)";   

</textarea>                    
                    						<p>
After these lines if you want to get the records from the Db you need this lines:
<textarea name="code" class="c#">
  
   storage.FillRecordCallback = new FillRecordHandler(FillRecordOrder);  
   
   OrdersVerticalBar[] res = storage.ExtractRecords() as OrdersVerticalBar[];
</textarea>
                    						<p>
                                                The FillRecord method is somethig like that:
                                                <textarea name="code" class="c#" id="TEXTAREA1" onclick="return TEXTAREA1_onclick()">

protected void FillRecordOrder(object rec, object[] fields)
{
	OrdersVerticalBar record = (OrdersVerticalBar) rec;

	record.OrderID = (int) fields[0];
	record.CustomerID = (string) fields[1];
	record.EmployeeID = (int) fields[2];
	record.OrderDate = (DateTime) fields[3];
	record.RequiredDate = (DateTime) fields[4];

	if (fields[5] != DBNull.Value)
		 record.ShippedDate = (DateTime) fields[5];
	else
 		 record.ShippedDate = DateTime.MinValue;

	record.ShipVia = (int) fields[6];
	record.Freight = (decimal) fields[7];
}
</textarea>
</blockquote>                     
                    <h2>
                        Write the results direct to a File</h2>
                    &nbsp; &nbsp;&nbsp; You need this code for that task:<br />
                    
<blockquote>
<textarea name="code" class="c#">
   
  FileDataLink.EasyExtractToFile(storage, "outfile.txt");

  // or

  FileDataLink link = new FileDataLink(storage); 
  link.ExtractToFile("outfile.txt");     
</textarea>
</blockquote>
                    <br />
                    <blockquote>
		{$FOOTER$}
                    </td>
                </tr>
		</table>

	
	</BODY>
</HTML>
